import globalMitt from '../../../utils/globalMitt.js';
const app = getApp()
Page({
  onLoad() {
    this.ctx = wx.createCameraContext()
  },
  data: {
    imgUrl: app.globalData.imgUrl,
    isRecording: false,
    time: 0,
    src: null,
    videoSrc: null
  },
  temp:null,
  timer:null,
  startRecord() {
    this.ctx.startRecord({
      success: (res) => {
        console.log('startRecord')
        this.setData({
          isRecording:true
        })
        this.setData({
          time:0
        })
        this.timer = setInterval(() => {
          this.setData({
            time: this.data.time + 1
          })
          if(this.data.time > 10 && this.data.isRecording){
            this.stopRecord()
            this.timer = null
          }
        },1000)
      }
    })
  },
  stopRecord() {
    if (this.timer) {
      clearInterval(this.timer)
    }
    this.ctx.stopRecord({
      success: (res) => {
        console.log(this.timer,'this.timer')
        this.temp = res
        this.setData({
          src: res.tempThumbPath,
          videoSrc: res.tempVideoPath
        })
        this.setData({
          isRecording: false
        })
      }
    })
  },
  error(e) {
    console.log(e.detail)
  },
  upload(){
    if(!this.temp){
      wx.showToast({
        title: '没有找到录像',
        icon: 'none'
      })
      return
    }
    globalMitt.emit('takeVideo', this.temp)
    wx.navigateBack({
      delta: 1,
    })
  },
  cancel(){
    this.temp = null
    this.setData({
      src: null,
      videoSrc: null
    })
  }
})